<template>
  <div>
    <el-col :span="24">
      <el-col :span="12">
    <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
      <el-tab-pane
        :key="item.jjxId"
        v-for="(item, index) in list"
        :label="item.jjxName"
        :name="item.jjxName"
      >
        
                <div>
                        <span>可分配余额：<span class="font22 blue">{{item.jjZe*1-item.ygJj*1}}</span></span>
                        <span class="ml30">已分配金额：<span class="font22 blue">{{item.sum*1==0?item.bmJj*1:item.sum*1}}</span></span>
                        <!-- -item.sum*1 item.bmJj*1+-->
                        <span class="ml30">剩余可分配额：<span class="font22 blue">{{item.jjZe*1-item.ygJj*1-(item.sum*1==0?item.bmJj*1:item.sum*1)}}</span></span>
                    </div>
                <el-table
                  :key="item.jjxId"
                  :data="item.tableData"
                  :ref="'multipleTable'+item.jjxId"
                  :span-method="objectSpanMethod"
                  border
                  max-height="350"
                  @selection-change="handleSelectionChange"
                  style="width: 100%; margin-top: 20px;">
                  <el-table-column
                    type="selection"
                    width="80">
                  </el-table-column>
                  <el-table-column
                    prop="jgFzName"
                  >
                    <template slot="header" slot-scope="scope">
                      <el-select v-model="jigou" @change="changejgFzName" clearable placeholder="机构分组">
                        <el-option
                          v-for="it in item.jigouList"
                          :key="it.id"
                          :label="it.jgFzName"
                          :value="it.id">
                        </el-option>
                      </el-select>
                    </template>
                    <template slot-scope="scope">
                      <span style="cursor: pointer;" @click="groupEditing(scope.row,'jg')">{{ scope.row.jgFzName }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column
                    prop="jjxMc"
                    label="奖项">
                    <template slot="header" slot-scope="scope">
                      <el-select v-model="jiangxiang" @change="changeJjxMcTwo" clearable placeholder="奖项">
                        <el-option
                          v-for="it in item.valueListTwo"
                          :key="it.id"
                          :label="it.jjxMc"
                          :value="it.id">
                        </el-option>
                      </el-select>
                    </template>
                    <template slot-scope="scope">
                      <span style="cursor: pointer;" @click="bonusDistribution(scope.row,item.jjxName)">{{ scope.row.jjxMc }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column
                    prop="jjxJe"
                    label="输入金额">
                    <template slot-scope="scope">
                      <el-input v-model="item.tableData[scope.$index].jjxJe" type="number" @blur="blurJjxje(scope.$index,scope.row.gsId)"
                                placeholder=""></el-input>
                    </template>
                  </el-table-column>
                  <el-table-column
                    align="center"
                    label="操作">
                    <template slot-scope="scope">
                      <el-button type="danger" @click="renSplice(scope.$index,item.tableData)">删除</el-button>
                    </template>
                  </el-table-column>
                </el-table>
      </el-tab-pane>
      </el-tabs>
      </el-col>
      <el-col :span="12">
                <!--部门保存-->
                <buMenKeepForm v-show="showJg&&tabName=='second'" ref="buMenKeepRef" @guan="guan"></buMenKeepForm>
                
      </el-col>
    <el-dialog
        title="提示"
        :visible.sync="buDialogVisible"
        width="100%"
        :modal='false'
        :fullscreen='true'
        center>
      <buMenKeepForm ref="buMenKeepRef" @guan="guan"></buMenKeepForm>
    </el-dialog>
    
    </el-col>
    <!--人员分组-->
    <groupEditingForm ref='groupEditingRef' @refreshDataList="refreshDataList"></groupEditingForm>
    <el-dialog title="奖金分配" width="90vw" height="90vh" top="5vh" append-to-body :close-on-click-modal="false"
               :visible.sync="jiangVisible" v-if="jiangVisible" @close="callbackTwo">
      <jiangjinxiangTable ref="jiangjinxiangRef" @jiangjinxChild="jiangjinxChild"></jiangjinxiangTable>

    </el-dialog>
  </div>
</template>

<script>
import groupEditingForm from '@/views/jiangjin/jiangjinfpjjfpdjb/groupEditing-form'
import jiangjinxiangTable from '@/views/jiangjin/jiangjinfpjjfpdjb/jiangjinxiang-table'
import buMenKeepForm from '@/views/jiangjin/jiangjinfpjjfpdjb/buMenKeep-form'
import {getRyfzbList,updateGrNew,getJgfzList,updateBmNew,saveTemplate,pageTemplate,getTemplateBySTa} from "@/api/jiangjin/jiangjinryfzb"
import {getObj, putObj, updateGr,getGsData} from '@/api/jiangjin/jiangjinfpjjfpdjb'
export default {
  components: {
    groupEditingForm,
    jiangjinxiangTable,
    buMenKeepForm
  },
  name: "buMen-table",
  props:{
    fromRy:{
      rydsJiang:0
    }
  },
  data() {
    return {
      activeName: "",
      list: [],
      multipleSelection: [],
      spanArr: [],
      pos: 0,
      jigouList: [],
      valueListTwo: [],
      jiangVisible: false,
      bonusList: [],
      jigou: "",
      jiangxiang: "",
      tableDataOld:[],
      showJg:false,
      jjfpId:"",
      deptId:"",
      params:{},
      listTwo:[],
      fpId:"",
      tabName:"",
      valueListThree:[],
      sfCeff:"",
      gqTime:"",
      buDialogVisible:false,
    }
  },
  methods: {
    //关闭页面
    guan(guan){
      if(guan=='关闭'){
        var buTableChild={
          guan:'关闭'
        }
        this.$emit("buTableChild",buTableChild)
      }
      if(guan=='全屏'){
          this.buDialogVisible=true;
          this.$refs.buMenKeepRef.init(this.jjfpId)
      }
        
    },
    init(data,jjfpId,deptId,tabName,sfCeff,gqTime) {
      this.listTwo = data
      this.jjfpId=jjfpId
      this.deptId=deptId
      this.tabName=tabName;
      this.sfCeff=sfCeff
      this.gqTime=gqTime
     ////console.log("this.tabName:"+this.tabName)
      this.fromRy.rydsJiang=this.fromRy.rydsJiang?this.fromRy.rydsJiang:0
      
      this.getCs()
    },
    getyue(){
            getObj(this.jjfpId).then(res=>{
                for(var i in this.list){
                    this.list[i].jjYe=JSON.parse(res.data.data.jjxJeXq)[this.list[i].jjxId].jjYe;
                    this.list[i].bmJj=JSON.parse(res.data.data.jjxJeXq)[this.list[i].jjxId].bmJj;
                    this.list[i].ygJj=JSON.parse(res.data.data.jjxJeXq)[this.list[i].jjxId].ygJj;
                    this.list[i].jjZe=JSON.parse(res.data.data.jjxJeXq)[this.list[i].jjxId].jjZe;
                }
               ////console.log("重新获取:"+JSON.stringify(res.data.data))
            })
    },
    getCs(){
      var params={
        fpId:this.jjfpId,
        fpType:1
      }
      getTemplateBySTa(params).then(res=>{
        
        if(res.data.data){
   
                    this.list=JSON.parse(res.data.data.fpData)
                    this.showJg=true
                    this.$nextTick(()=>{
                      this.$refs.buMenKeepRef.init(this.jjfpId)
                    })
                    this.activeName = this.list[0].jjxName;
                     //this.fanFU()
                     
                }else{
                    this.list=this.listTwo
                    this.activeName = this.list[0].jjxName;
                }
           
           //this.tableDataOld= this.list[0].tableData
           //this.valueListThree=this.shuaiXuan(this.list[0].tableData)
          //console.log("this.tableDataOld机构:"+JSON.stringify(this.list[0].tableData))  
          //console.log("this.valueListThree机构:"+JSON.stringify(this.valueListThree))  
           this.getSpanArr(this.list[0].tableData);
           this.getyue()
      })
    },
    //获取最新公式数据
    getGongs(data){
      var params={
        jjbId:data.jjbId,
        fzId:data.fzId,
        jjxId:data.jxId,
        fpType:'1'
      }
      getGsData(params).then(res=>{
       //console.log("GONGSI:"+JSON.stringify(res.data.data))
        if(res.data.data){
          for(var i in this.list){
            if(this.activeName==this.list[i].jjxName){
              for(var j in this.list[i].tableData){
                if(res.data.data.fzId==this.list[i].tableData[j].fzId&&res.data.data.jjxId==this.list[i].tableData[j].jjxId){
                    this.list[i].tableData[j].gsId=res.data.data.gsId*1
                    if(res.data.data.gsId=='-1'){
                      this.list[i].tableData[j].fzInfo=res.data.data.fzInfo;
                      this.list[i].tableData[j].fzInfo=this.list[i].tableData[j].fzInfo.map(item=>{
                        item.jjJe=0
                        return item
                      })
                    }
                }
              }
            }
          }
        }
      })
    },
    //复现选择的数据
    fanFU(){
     
        for(var i in this.list){
         
          if(this.activeName==this.list[i].jjxName){
            var jg=this.list[i].jigouList.map(item=>{
              return item.id
            })
            var jj=this.list[i].valueListTwo.map(item=>{
              return item.id
            })
            var buTableChild={
              fanH:true,
              jg:jg,
              jj:jj
            }
            this.toggleSelection(this.list[i].tableData)
            this.multipleSelection=this.list[i].tableData
            //console.log("this.multipleSelection:"+JSON.stringify(this.multipleSelection))
            
           //console.log("复现")
            this.$emit("buTableChild",buTableChild)
          }
        }
    },
    muban(data){
            for(var i in this.list){
                for(var j in data){
                    if(this.list[i].jjxId==data[j].jjxId){
                        this.list[i].tableData=data[j].tableData
                    }
                }
                if(this.activeName==this.list[i].jjxName){
                    this.getSpanArr(this.list[i].tableData);
                    if(this.list[i].tableData.length>0){
                        this.blurJjxje()
                    }
                }
            }
        },
    keepMuBan(data){
      var params={
                fpId:data.jjfpId,
                fpName:data.value,
                fpType:1,
                fpSaveType:"b",
                fpData:this.list,
            }
            saveTemplate(params).then(res=>{
                    if(res.data.code==0){
                        this.$message({
                            type: 'success',
                            message: '模板保存成功!'
                        });
                    }
                })
    },
    jiang(value, jigouList) {
      
      if (value.length > 0) {
        var list1 = []
        for (var i in this.list) {
          if(this.activeName==this.list[i].jjxName){
            this.list[i].jigouList = jigouList.filter(x => {
              return value.some(y => {
                return x.id == y;
              })
            })
             this.list[i].tableData = this.list[i].tableData.filter(item => value.includes(item.id))
             ////console.log("this.list444:"+JSON.stringify(this.list))
              var idSet1 = this.list[i].tableData.reduce((acc, v) => {
                acc[v.id] = true;
                return acc;
              }, {});

              // 遍历list2，去掉在idSet中存在的id
              var result1 = value.filter(v => !idSet1[v]);
              list1 = jigouList.filter(x => {
                return result1.some(y => {
                  return x.id == y;
                })
              })
            
            this.list[i].tableData = this.list[i].tableData.concat(list1)
            this.tableDataOld=JSON.parse(JSON.stringify(this.list[i].tableData)) 
             this.getSpanArr(this.list[i].tableData);
          }
          // this.tableDataOldTwo=this.tableDataTwo;
          // this.addConfirm()
          
        }
      } else {
        for (var i in this.list) {
          this.list[i].tableData = []
          this.list[i].jigouList=[]
          this.getSpanArr(this.list[i].tableData);
        }
      }
      var buTableChild={
          qingJ:true,
        }
      this.$emit("buTableChild",buTableChild);
    },
    //奖项
    jigoujiangjin(value, bonusList) {

      if (this.multipleSelection.length == 0) {
        this.$message.warning("请先选择哪些机构分组!")
        //this.form.jigou=[]
        return false;
      }
      this.bonusList = bonusList

      this.valueListTwo = this.bonusList.filter(x => {
        return value.some(y => {
          return x.id == y;
        })
      })

      
      let obj = {}, jjxMc, obj2 = {};
      var list = this.multipleSelection, arr = [], arr2 = [];
     
      if (value.length > 0) {
        for (var i in list) {
          for (var j in this.valueListTwo) {

            obj = {
              jjxMc: this.valueListTwo[j].jjxMc,
              jjxId: this.valueListTwo[j].id,
              tenantId: list[i].tenantId,
              sl: list[i].sl,
              id: list[i].id,
              zjDeptId: list[i].zjDeptId,
              zjDeptName: list[i].zjDeptName,
              fzXs: list[i].fzXs,
              jgFzName: list[i].jgFzName,
              deptId: list[i].deptId,
              dataState: list[i].dataState,
              createDeptId: list[i].createDeptId,
              createDeptName: list[i].createDeptName,
              createUserId: list[i].createUserId,
              createUserName: list[i].createUserName,
              createTime: list[i].createTime,
              fzId: list[i].id,
              fzName: list[i].jgFzName,
              jjxJe: 0,
              gsId: list[i].gsId,
              gsName: list[i].gsName,
              jjxDate: list[i].jjxDate,
              jkjbj: list[i].jkjbj,
              jjWs: list[i].jjWs,
              fzInfo:list[i].fzInfo,
            }
            //奖金项列表
            arr.push(obj)

          }
        }
      } else {
        for (var i in list) {
          obj = {
            jjxMc: "",
            jjxId: "",
            tenantId: list[i].tenantId,
            sl: list[i].sl,
            id: list[i].id,
            zjDeptId: list[i].zjDeptId,
            zjDeptName: list[i].zjDeptName,
            fzXs: list[i].fzXs,
            jgFzName: list[i].jgFzName,
            deptId: list[i].deptId,
            dataState: list[i].dataState,
            createDeptId: list[i].createDeptId,
            createDeptName: list[i].createDeptName,
            createUserId: list[i].createUserId,
            createUserName: list[i].createUserName,
            createTime: list[i].createTime,
            fzId: list[i].id,
            fzName: list[i].jgFzName,
            jjxJe: 0,
            gsId: list[i].gsId,
            gsName: list[i].gsName,
            jjxDate: list[i].jjxDate,
            jkjbj: list[i].jkjbj,
            jjWs: list[i].jjWs,
            fzInfo:list[i].fzInfo,
          }
          //奖金项列表
          arr.push(obj)
        }
      }

      const idSet = arr.reduce((acc, v) => {
        acc[v.id] = true;
        return acc;
      }, {});

      for (var n in this.list) {
          
        if (this.activeName == this.list[n].jjxName) {
          const result = this.list[n].tableData.filter(v => !idSet[v.id]);
          
          this.list[n].tableData = this.unique(arr.concat(result))
        //console.log("this.tableDataOld222:"+JSON.stringify(this.tableDataOld))
        
          for(var a in this.tableDataOld){
            for(var b in this.list[n].tableData){
              if(this.tableDataOld[a].id==this.list[n].tableData[b].id&&this.tableDataOld[a].jjxMc!=""&&this.list[n].tableData[b].jjxMc!=""){
                  this.list[n].tableData=this.unique(this.list[n].tableData.concat(this.tableDataOld[a]))
              }
            }
          }
          for(var z in this.valueListTwo){
            for(var e in this.multipleSelection){
              var params={
                  jjbId:this.list[n].jjxId,
                  fzId:this.multipleSelection[e].fzId,
                  jxId:this.valueListTwo[z].id,
              }
              this.getGongs(params)
            }
          }
     
          this.valueListThree=this.valueListThree.concat(this.valueListTwo)
          this.valueListThree=this.uniqueTwo(this.valueListThree)
           this.list[n].tableData=this.list[n].tableData.filter(item=>{
              return this.valueListThree.some(it=>{
                if(item.jjxMc!=''){
                  return it.jjxMc==item.jjxMc
                }else{
                  return item
                }
                
              })
          })
         this.list[n].valueListTwo=this.shuaiXuan(this.list[n].tableData)
         this.tableDataOld=this.list[n].tableData
          this.getSpanArr(this.list[n].tableData)
          //this.tableDataOldTwo=this.tableDataTwo;
          this.$nextTick(() => {
            
            //this.getSpanArrJiang(this.tableDataTwo)
          //  this.toggleSelection(arr)
            //this.multipleSelection = list;
            this.blurJjxje()
            this.$forceUpdate()
          })
        }
      }

    },
    //奖金筛选
    shuaiXuan(data){
      var arr=[]
      this.bonusList.forEach(x => {
               data.forEach(y => {
                 if(x.id == y.jjxId){
                   arr.push(x)
                 };
              })
            })
       return  this.uniqueTwo(arr)
    },
    //去重
    unique(arr) {
      for (var i = 0; i < arr.length; i++) {
        for (var j = i + 1; j < arr.length; j++) {
          if (arr[i].id == arr[j].id && arr[i].jjxId == arr[j].jjxId) {         //第一个等同于第二个，splice方法删除第二个
            arr.splice(j, 1);
            j--;
          }
        }
      }
     
      return arr;

    },
    //去重奖金项
    uniqueTwo(arr){
      for (var i = 0; i < arr.length; i++) {
        for (var j = i + 1; j < arr.length; j++) {
          if (arr[i].id == arr[j].id) {         //第一个等同于第二个，splice方法删除第二个
            arr.splice(j, 1);
            j--;
          }
        }
      }
      //console.log("arr:"+JSON.stringify(arr))
      return arr;
    },
    toggleSelection(rows) {
      var _this = this;
      
      for (var i in this.list) {
        if (this.activeName == this.list[i].jjxName) {
          var refs = 'multipleTable' + [this.list[i].jjxId];
    
          if (rows) {
              rows.forEach((row,index) => {
                 ////console.log(index,row)
                  this.$nextTick(() => {
                    _this.$refs[refs][0].toggleRowSelection(row);
                    })
              })
            
          } else {
            this.$refs[refs][0].clearSelection();
          }
        }

      }
      this.multipleSelection=rows

    },
    //人员与机构分组删除
    renSplice(index, data) {
      data.splice(index, 1)
      this.getSpanArr(data);
      //this.getSpanArrJiang(data)
    },
    //奖金分配
    bonusDistribution(row,jjxName) {
      this.jiangVisible = true
      this.$nextTick(() => {
        var params = {
          activeName: "second",
          row: row,
          jjxName:jjxName
        }
        this.$refs.jiangjinxiangRef.init(params)
      })
      // this.$refs.bonusDistributionRef.init(this.jjfpId)
    },
    blurJjxje(index,gsid) {
      if(gsid==""){
              this.$message.warning("请先选择计算公式!")
              return
          }
      this.getnum2()
      for(var i in this.list){
        // (this.list[i].jjYe*1-this.fromRy.rydsJiang*1)
       
       if(this.sfCeff=='否'){
          if(this.list[i].sum*1>(this.list[i].jjZe*1-this.list[i].ygJj*1)){
                this.list[i].tableData[index].jjxJe=0;
                this.$message.warning("已分配的金额大于"+this.activeName+"剩余可分配的金额!")
                this.getnum2()
                
            }
       }
       
        if(this.activeName==this.list[i].jjxName){
            this.getSpanArr(this.list[i].tableData);
        }
      
      }
    },
    getnum2(){
      for(var i in this.list){
        if(this.activeName==this.list[i].jjxName){
            var list=this.list[i].tableData.map(item=>{
              return  item.jjxJe*1
              // if(item.gsId==-1){
              //     return  item.jjxJe*item.sl*1
              // }else{
              //     return  item.jjxJe*1
              // }
            })
            this.list[i].sum= this.sum(list)
            this.list[i].jjYe=this.list[i].jjZe*1-this.list[i].sum*1
            var params={
              activeName:this.list[i].jjxName,
              jgnum:this.list,
              dsJiang:this.list[i].sum,
              jiSuan:true
            }
            this.$emit("buTableChild",params)
        }

        
      }
    },
    //数组总和
    sum(arr) {
          return arr.reduce(function(prev, curr, idx, arr){
              return prev*1 + curr*1;
          });
      },
    //机构分组筛选
    changejgFzName(value) {
      for(var i in this.list){
        if(this.activeName==this.list[i].jjxName){
          if(value!=""){
              var  arr=[]
              this.list[i].tableData=this.tableDataOld;
              for(var j in this.list[i].tableData){
                  if(this.list[i].tableData[j].id==value){
                      arr.push(this.list[i].tableData[j])
                  }
              }
              this.list[i].tableData=arr;
              this.getSpanArr(this.list[i].tableData)
          }else{
              this.list[i].tableData=this.tableDataOld;
              this.getSpanArr(this.list[i].tableData)
          }
        }
      }
      
    },
    //清除选项
    qingXuan(){
      for(var i in this.list){
        var refs = 'multipleTable' + [this.list[i].jjxId];
         this.$refs[refs][0].clearSelection();
      }
    },
    handleClick(tab) {
      var jiIds=[]
    
      for (var i in this.list) {
        if (tab.name == this.list[i].jjxName) {
          this.getSpanArr(this.list[i].tableData)
          if(this.list[i].tableData.length>0){
            this.blurJjxje()
          }
          if(this.list[i].tableData.length>0){
            jiIds=this.list[i].jigouList.map(item=>{
                return item.id
            })
          }else{
            jiIds=[]
          }
          
        var buTableChild={
          qingK:true,
          jiIds:jiIds
        }
        this.tableDataOld=this.list[i].tableData
        
        this.$emit("buTableChild",buTableChild);
        }
        var refs = 'multipleTable' + [this.list[i].jjxId];
         this.$refs[refs][0].clearSelection();

         
        
      }
      
    },
    changeJjxMcTwo(value) {
      for(var i in this.list){
      
        if(this.activeName==this.list[i].jjxName){
    
          if(value!=""){
              var  arr=[]
              
              
              this.list[i].tableData=this.tableDataOld
              for(var j in this.list[i].tableData){
               
                  if(this.list[i].tableData[j].jjxId==value){
                      arr.push(this.list[i].tableData[j])
                  }
              }
              
              this.list[i].tableData=arr;
              this.getSpanArr(this.list[i].tableData)
          }else{
              this.list[i].tableData=this.tableDataOld;
              this.getSpanArr(this.list[i].tableData)
          }
        }
      }
      
    },
    //人员分组
    groupEditing(row, type) {
      var params = {
        activeName: this.activeName,
        row: row,
        type: type
      }
      this.$refs.groupEditingRef.init(params)
    },
    //合并单元格
    objectSpanMethod({row, column, rowIndex, columnIndex}) {

      if (columnIndex === 0 || columnIndex === 1) {

        const _row = this.spanArr[rowIndex] ? this.spanArr[rowIndex] : 0;
        const _col = _row > 0 ? 1 : 0;

        return {
          // [0,0] 表示这一行不显示， [2,1]表示行的合并数
          rowspan: _row,
          colspan: _col
        };
      }
    },
    //人员分组合并单元格
    getSpanArr(data) {
      function sortData(obj1,obj2){
          return obj1.id-obj2.id
      }
      var dataArr=data.sort(sortData);
      this.spanArr = []
      for (var i = 0; i < dataArr.length; i++) {
        if (i === 0) {
          this.spanArr.push(1);
          this.pos = 0;
        } else {
          // 判断当前元素与上一个元素是否相同
          if (dataArr[i].id === dataArr[i - 1].id) {
         
            this.spanArr[this.pos] += 1;
            this.spanArr.push(0);
          } else {
            this.spanArr.push(1);
            this.pos = i;
          }
        }

      }
    },
    //从奖金项子集过来
    jiangjinxChild(jiangjinxChild){
        this.jiangVisible=false;
        for(var i in this.list){
          if(this.activeName==this.list[i].jjxName){
              this.list[i].tableData=this.list[i].tableData.map(item=>{
                if(item.id==jiangjinxChild.id&&item.jjxId==jiangjinxChild.jjxId){
                    return jiangjinxChild
                }else{
                    return item
                }
            })
           // this.getSpanArrJiang(this.list[i].tableData)
          }
        }
        //console.log("返回this.list:"+JSON.stringify(this.list))
    },
    //表格多选
    handleSelectionChange(val) {
       this.multipleSelection=val;
        var fzIds=[],jjxIds=[]
        // for(var i in this.list){
        //   if(this.activeName==this.list[i].jjxName){
        //     this.list[i].tableData.forEach(item=>{
        //      this.multipleSelection.forEach(it=>{
        //        if(it.fzId==item.fzId){
        //          fzIds.push(item)
        //        }
        //      })
        //   })
        //   }
          
           
        //     fzIds=this.unique(fzIds)
        //     fzIds.forEach(item=>{
        //         jjxIds.push(item.jjxId)
        //     })
            
        //    //console.log("表格多选")
           
        // }
        // var buTableChild={
        //       fanXuan:true,
        //       jjxIds:jjxIds
        //     }
        //  this.$emit("buTableChild",buTableChild)
        
    },
    callbackTwo() {
      this.jiangVisible = false
    },
    refreshDataList() {

    },
    putupdateBmNew(data){
      var jjData=[],fpData=[],fzInfo=[],fzInfoJe=0;
      this.fpId=data.fpId
      for(var i in this.list){
       
            var list=this.list[i].tableData.filter(item=>{
                return item.jjxMc==""
            })
            var gongshi=this.list[i].tableData.filter(item=>{
                return item.gsId==""
            })
            if(list.length>0){
                this.$message.warning("存在机构分组未选择奖金项!")
                return false;
            }
            if(gongshi.length>0){
                this.$message.warning("存在机构分组未选择公式!")
                return false;
            }
            var obj={
              jjxxqId:this.list[i].jjxId,
              amt:this.list[i].sum
            }
            jjData.push(obj)
            for(var j in this.list[i].tableData){
                this.list[i].tableData[j].jjxxqId=this.list[i].jjxId;
                if(this.list[i].tableData[j].gsId==-1){
                 
                  fzInfo=this.list[i].tableData[j].fzInfo.map(item=>{
                     return item.jjJe*1
                  })
                  fzInfoJe=this.sum(fzInfo);
                  if(this.list[i].tableData[j].jjxJe*1!=fzInfoJe){
                      var je=this.list[i].tableData[j].jjxJe*1/this.list[i].tableData[j].sl*1;
                    
                      for(var n in this.list[i].tableData[j].fzInfo){
                        this.$set(this.list[i].tableData[j].fzInfo[n],'jjJe',je)
                      }
                      
                  }
                  
                }
                fpData.push(this.list[i].tableData[j])
                
            }
            
          
      }

           
     
            this.params={
                fpId:this.fpId,
                fpData:fpData,
                jjYe:data.jjYe,
                jjData:jjData
            }
            this.$confirm('是否将该条数据保存为模板, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
                this.queRen()
            }).catch(() => {
               this.keepShuJu()      
            });
           
            
    },
    queRen(){
      this.$prompt('请输入模板名称', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
            }).then(({ value }) => {

                if(value==""){
                    this.$message.warning("请填写模板名称!")
                    return false
                }
                var params={
                    jjfpId:this.jjfpId,
                    value:value,
                }
                this.keepMuBan(params)
                this.keepShuJu()
            }).catch(() => {
            this.$message({
                type: 'info',
                message: '取消输入模板名称'
            });
            });
    },
    keepShuJu(){
       const loading = this.$loading({
                lock: true,
                text: `正在计算中,请稍后......`,
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            })
            var params={
                fpId:this.fpId,
                fpName:"默认",
                fpType:1,
                fpSaveType:"a",
                fpData:this.list,
            }
     
            saveTemplate(params).then(res=>{
                updateBmNew(this.params).then(res=>{
                    //console.log(JSON.stringify(res))
                    if(res.data.code==0){
                      
                        this.showJg=true
                        this.$nextTick(()=>{
                            //console.log("this.jjfpId:"+this.jjfpId)
                            this.$refs.buMenKeepRef.init(this.fpId)
                            this.getyue()
                            // for(var i in this.list){
                            //   this.list[i].sum=0
                            //     this.list[i].tableData=[]
                            // }
                        })
                        loading.close()
                    }else{
                      loading.close()
                    }

                })
                })
        },
    
  }

}
</script>

<style>

</style>
